package org.dashboard.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdPage;
import org.dashboard.bean.AdUserPage;
import org.dashboard.constant.Constant;
import org.dashboard.dao.AdPageMapper;
import org.dashboard.dao.AdUserPageMapper;
import org.dashboard.service.AdPageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = AdPageService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER, cacheNames = "page")
public class AdPageServiceImpl implements AdPageService {
    private static final Logger logger = LoggerFactory.getLogger(AdPageServiceImpl.class);

    @Autowired
    AdPageMapper adPageMapper;

    @Override
    @CacheEvict(key = "'pageId:' + #pageId.toString()")
    public int deleteByPrimaryKey(Integer pageId) {
        return adPageMapper.deleteByPrimaryKey(pageId);
    }

    @Override
    @CacheEvict(key = "'pageId:' + #record.pageId.toString()")
    public int insert(AdPage record) {
        adPageMapper.insert(record);
        return record.getPageId();
    }

    @Override
    @CacheEvict(key = "'pageId:' + #record.pageId.toString()")
    public int insertSelective(AdPage record) {
        adPageMapper.insertSelective(record);
        return record.getPageId();
    }

    @Override
    @Cacheable(key = "'pageId:' + #pageId.toString()")
    public AdPage selectByPrimaryKey(Integer pageId) {
        return adPageMapper.selectByPrimaryKey(pageId);
    }

    @Override
    @CacheEvict(key = "'pageId:' + #record.pageId.toString()")
    public int updateByPrimaryKeySelective(AdPage record) {
        return adPageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @CacheEvict(key = "'pageId:' + #record.pageId.toString()")
    public int updateByPrimaryKeyWithBLOBs(AdPage record) {
        return adPageMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    @CacheEvict(key = "'pageId:' + #record.pageId.toString()")
    public int updateByPrimaryKey(AdPage record) {
        return adPageMapper.updateByPrimaryKey(record);
    }


    @Override
    public List<AdPage> selectAdPages() {
        return adPageMapper.selectAdPages();
    }

    @Override
    @Transactional
    @CacheEvict(allEntries = true)
    public boolean deleteByPrimaryKeys(ArrayList<Integer> pageIds) {
        for (Integer id : pageIds) {
            adPageMapper.deleteByPrimaryKey(id);
        }
        return true;
    }

    @Override
    public PageInfo selectAdPagesPaged(String criteria, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdPage> adPages = adPageMapper.selectAdPagesSearched(criteria);
        return new PageInfo<>(adPages);
    }

}
